package com.onlinemusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onlinemusic.common.R;
import com.onlinemusic.mapper.SingerMapper;
import com.onlinemusic.pojo.domain.Singer;
import com.onlinemusic.pojo.request.SingerRequest;
import com.onlinemusic.service.ObjectStoreService;
import com.onlinemusic.service.SingerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Service
public class SingerServiceImpl extends ServiceImpl<SingerMapper, Singer> implements SingerService {
    @Autowired
    private SingerMapper singerMapper;

    @Autowired
    private ObjectStoreService oss;

    /**
     * 获取所有歌手 用于客户端展示
     *
     * @return
     */
    @Override
    public R allSinger() {
        return R.success("查询成功", singerMapper.selectList(null));
    }

    /**
     * 新增歌手
     *
     * @param addSingerRequest
     * @return
     */
    @Override
    public R addSinger(SingerRequest addSingerRequest) {
        //封装参数
        Singer singer = new Singer();
        BeanUtils.copyProperties(addSingerRequest, singer);
        //补齐数据
        String pic = "/img/avatarImages/user.jpg";
        singer.setPic(pic);
        if (singerMapper.insert(singer) > 0) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    /**
     * 删除歌手
     *
     * @param id
     * @return
     */
    @Override
    public R deleteSinger(Integer id) {
        //直接调用api删除
        if (singerMapper.deleteById(id) > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

//    public R deleteSinger(Integer id) {
//        Singer singer = singerMapper.selectById(id);
//        String obj = singer.getPic();
//        if (singerMapper.deleteById(id) > 0) {
//            boolean flag = oss.deleteFile(obj);
//            System.out.print("删除OSS文件：" + flag);
//            return R.success("删除成功");
//        } else {
//            return R.error("删除失败");
//        }
//    }

    /**
     * 批量删除歌手
     *
     * @param ids
     * @return
     */
    @Override
    public R deleteSingers(String[] ids) {

        //把数组中参数组织成SQL语句需要的格式
        StringBuffer sb = new StringBuffer();
        for (String i : ids) {
            sb.append(i + ",");
        }
        //删除最后一个逗号
        sb.deleteCharAt(sb.length() - 1);
        if (singerMapper.deleteByIds(sb.toString()) > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    /**
     * 修改歌手
     *
     * @param updateSingerRequest
     * @return
     */
    @Override
    public R updateSingerMsg(SingerRequest updateSingerRequest) {
        //拷贝
        Singer singer = new Singer();
        BeanUtils.copyProperties(updateSingerRequest, singer);
        //直接调用api
        if (singerMapper.updateById(singer) > 0) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }

    /**
     * 修改歌手图片
     *
     * @param avatarFile
     * @param id
     * @return
     */
    @Override
    public R updateSingerPic(MultipartFile avatarFile, int id) {
        String fileName = System.currentTimeMillis() + "-" + avatarFile.getOriginalFilename();
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "img"
                + System.getProperty("file.separator") + "singerPic";
        File file1 = new File(filePath);
        //不存在->创建目录
        if (!file1.exists()) {
            file1.mkdir();
        }

        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
        String imgPath = "/img/singerPic/" + fileName;
//        String imgPath = "img/singerPic";

        try {
            avatarFile.transferTo(dest);
        } catch (IOException e) {
            return R.fatal("上传失败" + e.getMessage());
        }


        // 使用阿里云对象存储服务替换原来的存储在本地的流程
//        String ossFilePath;
//        try {
//            ossFilePath = oss.uploadFile(imgPath, fileName, avatarFile);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }

        //补全数据
        Singer singer = new Singer();
        singer.setId(id);
        singer.setPic(imgPath);
        if (singerMapper.updateById(singer) > 0) {
            return R.success("上传成功", imgPath);
        } else {
            return R.error("上传失败");
        }
    }

    @Override
    public R singerOfName(String name) {
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        return R.success(null, singerMapper.selectList(queryWrapper));
    }

    @Override
    public R singerOfSex(Integer sex) {
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("sex", sex);
        return R.success(null, singerMapper.selectList(queryWrapper));
    }
}
